// 导入
import {
  isArray,
  isFunction,
  isObject,
  isString,
  isDate,
  isPromise,
  isSymbol,
} from '@vue/shared'
import { isNil } from 'lodash-unified'
import { Fragment, Text, Comment } from 'vue'

// 导出
export { isArray, isFunction, isObject, isString, isDate, isPromise, isSymbol }

// 自定义类型检查函数
export const isUndefined = val => val === undefined
export const isBoolean = val => typeof val === 'boolean'
export const isNumber = val => typeof val === 'number'

// 检查值是否为空
export const isEmpty = val =>
  (!val && val !== 0) ||
  (isArray(val) && val.length === 0) ||
  (isObject(val) && !Object.keys(val).length)

// 检查值是否是 Element 类的实例
export const isElement = e =>
  typeof Element !== 'undefined' && e instanceof Element

// 检查属性是否为 null 或 undefined
export const isPropAbsent = prop => isNil(prop)

// 检查字符串是否可以转换为数字而不产生 NaN
export const isStringNumber = val => isString(val) && !Number.isNaN(Number(val))

// VNode 类型检查函数
export function isFragment(node) {
  return isVNode(node) && node.type === Fragment
}

export function isText(node) {
  return isVNode(node) && node.type === Text
}

export function isComment(node) {
  return isVNode(node) && node.type === Comment
}

const TEMPLATE = 'template'
export function isTemplate(node) {
  return isVNode(node) && node.type === TEMPLATE
}

// 检查 VNode 是否表示有效元素类型
export function isValidElementNode(node) {
  return isVNode(node) && !isFragment(node) && !isComment(node)
}

// 修补标志枚举
export const PatchFlags = {
  TEXT: 1,
  CLASS: 2,
  STYLE: 4,
  PROPS: 8,
  FULL_PROPS: 16,
  HYDRATE_EVENTS: 32,
  STABLE_FRAGMENT: 64,
  KEYED_FRAGMENT: 128,
  UNKEYED_FRAGMENT: 256,
  NEED_PATCH: 512,
  DYNAMIC_SLOTS: 1024,
  HOISTED: -1,
  BAIL: -2,
}

// 检查值是否既不是 null 也不是 undefined
export function isNotNullAndNotUndefined(val) {
  return val !== undefined && val !== null
}
